package com.monkey.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: zhangqi
 * Date: 2024/8/24
 *
 * @Description:3个线程从顺序打印0-100
 */

public class Print0_100Practice {

    public static  volatile int currentNum=0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(currentNum);
                currentNum=currentNum+1;
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(currentNum);
                currentNum=currentNum+1;
            }
        });

        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t2.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(currentNum);
                currentNum=currentNum+1;
            }
        });

        t1.start();
        t2.start();
        t3.start();

        t3.join();
        System.out.println("Main执行完成!!!");
    }
}
